/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.entity.EleSourceGroup;
import com.thinkgem.jeesite.modules.source.dao.EleSourceGroupDao;
import com.thinkgem.jeesite.modules.source.entity.EleSource;
import com.thinkgem.jeesite.modules.source.dao.EleSourceDao;

/**
 * 节目包管理Service
 * @author ZC
 * @version 2017-05-25
 */
@Service
@Transactional(readOnly = true)
public class EleSourceGroupService extends CrudService<EleSourceGroupDao, EleSourceGroup> {

	@Autowired
	private EleSourceDao eleSourceDao;
	
	public EleSourceGroup get(String id) {
		EleSourceGroup eleSourceGroup = super.get(id);
		eleSourceGroup.setEleSourceList(eleSourceDao.findList(new EleSource(eleSourceGroup)));
		return eleSourceGroup;
	}
	
	public List<EleSourceGroup> findList(EleSourceGroup eleSourceGroup) {
		return super.findList(eleSourceGroup);
	}
	
	public Page<EleSourceGroup> findPage(Page<EleSourceGroup> page, EleSourceGroup eleSourceGroup) {
		return super.findPage(page, eleSourceGroup);
	}
	
	@Transactional(readOnly = false)
	public void save(EleSourceGroup eleSourceGroup) {
		super.save(eleSourceGroup);
		for (EleSource eleSource : eleSourceGroup.getEleSourceList()){
			if (eleSource.getId() == null){
				continue;
			}
			if (EleSource.DEL_FLAG_NORMAL.equals(eleSource.getDelFlag())){
				if (StringUtils.isBlank(eleSource.getId())){
					eleSource.setGroup(eleSourceGroup);
					eleSource.preInsert();
					eleSourceDao.insert(eleSource);
				}else{
					eleSource.preUpdate();
					eleSourceDao.update(eleSource);
				}
			}else{
				eleSourceDao.delete(eleSource);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(EleSourceGroup eleSourceGroup) {
		super.delete(eleSourceGroup);
		eleSourceDao.delete(new EleSource(eleSourceGroup));
	}
	
}